package com.bookstore.dao.impl;

import com.bookstore.dao.OrderDao;
import com.bookstore.entity.Order;

import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int insert(Order order) {
        String sql = "INSERT INTO `T_order`(`money`,`receiverAddress`,`receiverName`,`receiverPhone`,`paystate`,`ordertime`,`user_id`) VALUES (?, ?, ?, ?, ? ,?,?)";
        return 0;
    }

    @Override
    public int delete(Integer orderId) {
        String sql = "DELETE FROM `T_order`WHERE `id`=?";
        return 0;
    }

    @Override
    public int update(Order order) {
        int result = 0;
        String sql = "UPDATE `T_order`SET";
        StringBuilder sb = new StringBuilder(sql);
        int flag=0;
        if(order==null){
            return result;
        }
        if(order.getMoney()!=null){
            flag=1;
            sb.append(" `money`=?");
        }
        if(order.getReceiverAddress()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `receiverAddress`=?");
            flag=1;
        }
        if(order.getReceiverName()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `receiverName`=?");
            flag=1;
        }
        if(order.getReceiverPhone()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `receiverPhone`=?");
            flag=1;
        }
        if(order.getPaystate()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `paystate`=?");
            flag=1;
        }
        if(order.getOrdertime()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `ordertime`=?");
            flag=1;
        }
        if(order.getUserId()!=null){
            if(flag!=0){
                sb.append(",");
            }
            sb.append(" `user_id`=?");
            flag=1;
        }
        return 0;
    }

    @Override
    public List<Order> queryOrder(Order order) {
        String sql = "SELECT * FROM `T_order` WHERE  1=1";
        StringBuilder sb = new StringBuilder(sql);
        if(order.getId()!=null){
            sb.append(" AND `id`=?");
        }
        if(order.getMoney()!=null){
            sb.append(" AND `money`=?");
        }
        if(order.getReceiverAddress()!=null){
            sb.append(" AND `receiverAddress`=?");
        }
        if(order.getReceiverName()!=null){
            sb.append(" AND `receiverName`=?");
        }
        if(order.getReceiverPhone()!=null){
            sb.append(" AND `receiverPhone`=?");
        }
        if(order.getPaystate()!=null){
            sb.append(" AND `paystate`=?");
        }
        if(order.getOrdertime()!=null){
            sb.append(" AND `ordertime`=?");
        }
        if(order.getUserId()!=null){
            sb.append(" AND `user_id`=?");
        }
        return null;
    }
}
